package com.sg.zhui.visitor.http;

import android.app.Activity;
import android.text.TextUtils;
import android.util.Log;
import android.widget.Toast;

import com.sg.zhui.visitor.activity.ApplyForAndAccreditRecordActivity;
import com.zhy.http.okhttp.callback.StringCallback;

import org.json.JSONObject;

import okhttp3.Call;
import okhttp3.Request;

/**
 * Created by wondercupid on 2018/1/5.
 */

public class BaseStringCallback_Visit extends StringCallback {

    ApplyForAndAccreditRecordActivity mContext;

    AsyncRequest mAsyncRequest;


    public BaseStringCallback_Visit(ApplyForAndAccreditRecordActivity context, final AsyncRequest callBackRequest) {

        mContext = context;
        mAsyncRequest = callBackRequest;
    }


    @Override
    public void onBefore(Request request, int id) {

        if (null != mContext) {
            mContext.showProgressDialog();
        }
    }

    @Override
    public void onAfter(int id) {
        if (null != mContext) {
            mContext.hideProgressDialog();
        }
    }

    @Override
    public void onError(Call call, Exception e, int id) {

        e.printStackTrace();
        if (null != mContext) {
            mContext.hideProgressDialog();
        }
        Log.d("TAG", id + "onError" + e.getMessage());

        if (!TextUtils.isEmpty(e.getMessage()) && e.getMessage().contains("Failed to connect")) {
            if (null != mContext) {
                ((Activity) mContext).runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        //此时已在主线程中，可以更新UI了
                        Toast.makeText(mContext, "请检查网络连接！", Toast.LENGTH_SHORT).show();
                    }
                });
            }
        } else if (!TextUtils.isEmpty(e.getMessage()) && e.getMessage().contains("failed to connect to") && e.getMessage().contains("after")) {
            if (null != mContext) {
                ((Activity) mContext).runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        //此时已在主线程中，可以更新UI了
                        Toast.makeText(mContext, "连接超时！", Toast.LENGTH_SHORT).show();
                    }
                });
            }
        } else {
            mAsyncRequest.RequestError(id, id, e.toString());
        }

    }

    @Override
    public void onResponse(String response, int id) {

        Log.e("TAG", "onResponse：complete" + response);
        if (null != mContext) {
            mContext.hideProgressDialog();
        }
        try {

            JSONObject jsonObject = new JSONObject(response);

            if (null != jsonObject) {
                mAsyncRequest.RequestComplete(id, response);
            } else {
                mAsyncRequest.RequestError(id, id, "jsonObject为null");
            }


        } catch (Exception e) {
            e.printStackTrace();
            mAsyncRequest.RequestError(id, id, e.toString());
        }

        switch (id) {
            case 100:
                break;
            case 101:
                break;
        }
    }

    @Override
    public void inProgress(float progress, long total, int id) {

        Log.e("TAG", id + " ---inProgress:" + progress);

//            mProgressBar.setProgress((int) (100 * progress));
//            @SuppressLint("InflateParams") View view = LayoutInflater.from(getApplicationContext()).inflate(R.layout.item_pager_entrance, null);


    }
}